package com.defect.service.basicsService.basicsServiceImpl;

import com.defect.bo.RoleBo;
import com.defect.mapper.*;
import com.defect.po.Role;
import com.defect.po.RoleStateRelKey;
import com.defect.po.RoleTypeRel;
import com.defect.service.basicsService.BRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BRoleServiceImpl implements BRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleStateRelMapper roleStateRelMapper;
    @Resource
    private RoleTypeRelMapper roleTypeRelMapper;

    /**
     * 查询所有角色
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Role> selectRoleAll() throws Exception {
        return roleMapper.selectRoleAll();
    }

    /**
     * 修改角色
     *
     * @param role
     * @throws Exception
     */
    @Override
    public void updateRole(Role role) throws Exception {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 增加 角色、角色状态关联表、角色类型关联表
     *
     * @param role
     * @throws Exception
     */
    @Override
    public void insertRole(RoleBo role) throws Exception {
        if(role.getRole() != null){
            roleMapper.insertSelective(role.getRole());
        }
        String[] states = role.getArr1();
        String[] types = role.getArr2();
        if (states != null && states.length != 0) {
            for (int i = 0; i < states.length; i++) {
                RoleStateRelKey s = new RoleStateRelKey();
                s.setRoleCode(role.getRole().getRoleCode());
                s.setIssueState(states[i]);
                roleStateRelMapper.insertSelective(s);
            }
        }
        if (types != null && types.length != 0) {
            for (int i = 0; i < types.length; i++) {
                RoleTypeRel t = new RoleTypeRel();
                t.setRoleCode(role.getRole().getRoleCode());
                t.setTypeCode(types[i]);
                roleTypeRelMapper.insertSelective(t);
            }
        }
    }

    /**
     * 根据ID删除角色
     * @param id
     * @throws Exception
     */
    @Override
    public void deleteRole(String id) throws Exception {
        roleMapper.deleteByPrimaryKey(id);
//        roleStateRelMapper.deleteByRoleCode(id);
//        roleTypeRelMapper.deleteByRoleCode(id);
    }
}
